package HindiMorphGen;

sub morphGen {

	my $root = $_[0];
	my $cat = $_[1];
	my $vib = $_[2];
	my $tam = $_[3];
	my $gnp = $_[4];


	$gnp=~/(.)(.)(..)(.)/;
	my $gen=$1;
	my $num=$3;
	my $per=$2;
	my $case=$4;

	my @t = split(/_+/,$tam);
	$tam=shift @t;
	if($tam eq "" || $tam eq "NULL") {
		$tam = "0";
	}

	my @v = split(/_+/,$vib);

#	print STDERR "$root $cat $vib $tam $gnp\n";
	

#------- CATEGORY CORRECTIONS ---------

	if($cat=~/^V/ || $cat eq "NBG" || $cat eq "MD") {
		$cat = "v";
	}
	elsif($cat=~/^N/) {
		$cat = "n";
	}
	elsif($cat=~/^J/) {
		$cat = "Adj";
	}
	elsif($cat=~/^PRP/ || $cat=~/^DEM/ || $cat eq "WP") {
		$cat = "P";
		if($vib ne "NULL") {
			$tam = $vib;  
			@v=();
		}
	}
	elsif($cat eq "TO" || $cat eq "IN" || $cat eq "RP") {
		$cat = "psp";
	}
	elsif($cat eq "CC") {
		$cat = "conj";
	}
	elsif($cat=~/^R/ || $cat eq "WRB") {
		$cat = "Adv";
	}
	elsif($cat eq "CD" || $cat eq "WDT" || $cat eq "WP\$" || $cat eq "DT" || $cat eq "PDT" || $cat eq "POS") {
		$cat = "det";
	}
	else {
		$cat = "n";
	}


# ------ GENDER CORRECTIONS -------------

	if($gen eq "" or ($gen ne "m" and $gen ne "f")) {
		$gen = "m";
	}

# ------ NUMBER CORRECTIONS -------------

	if($num eq "sg") {
		$num = "s";
	}
	elsif($num eq "pl") {
		$num = "p";
	}
	else {
		$num = "s";
	}

# ------ PERSON CORRECTIONS -------------

	if($per eq "1") {
		$per = "u";
	}
	elsif($per eq "2") {
		$per = "m";
	}
	elsif($per eq "3") {
		$per = "a";
	}
	else {
		$per = "a";
	}

# ------ CASE CORRECTIONS -------------

	if($case eq "o" and $cat eq "P") {
		if($tam eq "" or $tam eq "0") {
			$tam = "1";
		}
	}
	elsif($case eq "o" and $cat eq "Adj") {
		if($tam eq "" or $tam eq "0") {
			$tam = "1";
		}
	}
	$case = "o" if($case eq "");


# Generate LWG

	$out = &runGenerator($root,$cat,$gen,$num,$per,$case,$tam);

	for $tamComp (@t) {
		#Currently, morph is running only the first component of TAM
		#Hence, the following line is commented
		#$out.=" ".&runGenerator($tamComp,"v",$gen,$num,$per,$case,"0");

		$out.=" $tamComp";
	}

	for $vibComp (@v) {
		#Currently, morph is not run on Vibhakti in training data
		#Hence, the following line is commented
		#$out.=" ".&runGenerator($vibComp,"psp",$gen,$num,$per,$case,"0");

		$out.=" $vibComp" if($vibComp ne "NULL");

	}


	if($out ne "" and $cat ne "punc" and $cat ne "adv")
	{
		$out=~s/\s*$//g;
		return $out;
	}
	else
	{
		return $root;
	}

	
}

sub runGenerator {
		my ($root,$cat,$gen,$num,$per,$case,$tam) = @_;
		my $out;

		if($cat eq "psp" and $root eq "kA") {
			if($gen eq "f") {
				return "kI";
			}
			elsif(($num eq "s" and $case eq "o") or $num eq "p" ) {
				return "ke";
			}
			else {
				return "kA";
			}
		}

		my @c = split(/_+/,$root);
		my $input = $c[$#c]." ".$cat." $gen $num $per $case ".$tam;


		open(out2,">/tmp/hindi_gen_mid.tmp1$$");
		print out2 $input."\n";
		system("$ENV{'HindiMorphGen'}/gen/hnd_gen.out ON NOT < /tmp/hindi_gen_mid.tmp1$$ > /tmp/hindi_gen_mid.tmp$$ ");

		open(out1,"/tmp/hindi_gen_mid.tmp$$");
		$out = <out1>;
		system("rm -rf /tmp/hindi_gen_mid.tmp1$$ /tmp/hindi_gen_mid.tmp$$");
		chomp($out);

		$out=~s/^\s*//g;
		$out=~s/\@//g;
		$out=~s/\{.*//g;
		$out=~s/\_0$//g;
		$out=~s/\_1$//g;
		$out=~s/1$//g if(!$out=~/^[0-9]+$/);
		$out=~s/\_/ /g;

		$out = $c[$#c] if($out eq "");

		my $cc = $#c-1;
		$out = join(' ',@c[0..$cc])." ".$out if(@c>1);

		#print STDERR "$input   $out\n";

		return $out;
}

1;
